//线程同步
#include <iostream>
#include <string>
#include <unistd.h>
#include <pthread.h>
using namespace std;

pthread_cond_t gcond =PTHREAD_COND_INITIALIZER;
pthread_mutex_t gmutex =PTHREAD_MUTEX_INITIALIZER;
void* active(void* args)
{
    string name=(const char*)args;
    
    while(true)
    {
        pthread_mutex_lock(&gmutex);//先加锁
        pthread_cond_wait(&gcond,&gmutex);//然后等待，连入到等待队列中，然后等待抢到锁
        cout<< name<<" active !"<<endl;
        pthread_mutex_unlock(&gmutex);
    }
    return (void*)0;
}
int main()
{
    pthread_t t1,t2,t3,t4;
    pthread_create(&t1,nullptr,active,(void*)"thread-1");
    pthread_create(&t2,nullptr,active,(void*)"thread-2");
    pthread_create(&t3,nullptr,active,(void*)"thread-3");
    pthread_create(&t4,nullptr,active,(void*)"thread-4");
    

    sleep(3);
    //唤醒
    while(true)
    {
        pthread_cond_signal(&gcond);
        sleep(1);
    }

    pthread_join(t1,nullptr);
    pthread_join(t2,nullptr);
    pthread_join(t3,nullptr);
    pthread_join(t4,nullptr);
}

